package com.tanhua.server.controller;

import com.tanhua.domain.vo.CountsVo;
import com.tanhua.domain.vo.FriendVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息控制层
 */
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    /**
     * 查询用户信息
     * Long userID: 用户ID, 如果用户自己不传入
     * Long huanxinID: 环信用户账号id
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity getUserInfo(@RequestHeader("Authorization") String userToken, Long userID, Long huanxinID){
        //定义传入的UserId
        Long inputUserId = null;
        if(userID != null){
            inputUserId = userID;
        }
        else if(huanxinID != null){
            inputUserId = huanxinID;
        }
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userToken,inputUserId);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户信息
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity updateUserInfo(@RequestHeader("Authorization") String userToken,@RequestBody UserInfoVo userInfoVo){
        userInfoService.updateUserInfo(userToken,userInfoVo);
        return ResponseEntity.ok(null);
    }


    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @RequestMapping(value = "/counts",method = RequestMethod.GET)
    public ResponseEntity counts(){
        CountsVo vo  = userService.counts();
        return ResponseEntity.ok(vo);
    }


    /**
     * 查询相互喜欢、我喜欢、粉丝列表分页查询
     */
    @RequestMapping(value = "/friends/{type}",method = RequestMethod.GET)
    public ResponseEntity queryUserLikeList(@PathVariable("type") int type,
                                            @RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "10") int pagesize) {
        PageResult<FriendVo> pageResult  = userService.queryUserLikeList(type,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 粉丝-喜欢
     * 请求连接：POST  /fans/:uid
     */
    @PostMapping("/fans/{id}")
    public ResponseEntity fansLike(@PathVariable("id") Long likeUserId) {
        userService.fansLike(likeUserId);
        return ResponseEntity.ok(null);
    }
}
